package com.ssy.lingxi.member.merchant.serviceimpl.web;

import com.ssy.lingxi.common.constant.member.MemberStatusEnum;
import com.ssy.lingxi.common.constant.member.MemberTypeEnum;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.config.ServiceConfig;
import com.ssy.lingxi.member.merchant.entity.MemberDO;
import com.ssy.lingxi.member.merchant.entity.MemberLevelRightDO;
import com.ssy.lingxi.member.merchant.entity.MemberRelationDO;
import com.ssy.lingxi.member.merchant.model.bo.WorkflowTaskListBO;
import com.ssy.lingxi.member.merchant.model.constant.MemberOuterStatusEnum;
import com.ssy.lingxi.member.merchant.model.constant.PlatformInnerStatusEnum;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.PlatformMemberDetailBasicVO;
import com.ssy.lingxi.member.merchant.model.vo.validate.request.MemberValidateVO;
import com.ssy.lingxi.member.merchant.model.vo.validate.response.WorkFlowStepVO;
import com.ssy.lingxi.member.merchant.repository.MemberRelationRepository;
import com.ssy.lingxi.member.merchant.repository.MemberRepository;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberHistoryService;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberRegisterDetailService;
import com.ssy.lingxi.member.merchant.service.feign.IWorkflowFeignService;
import com.ssy.lingxi.member.merchant.service.web.IPlatformMemberDetailBasicService;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 平台后台 - 会员维护 - 会员详情 - 基本信息服务接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-07-16
 */
@Service
public class PlatformMemberDetailBasicServiceImpl implements IPlatformMemberDetailBasicService {
    @Resource
    private MemberRepository memberRepository;

    @Resource
    private IBaseMemberRegisterDetailService baseMemberRegisterDetailService;

    @Resource
    private IBaseMemberHistoryService baseMemberHistoryService;

    @Resource
    private MemberRelationRepository relationRepository;

    @Resource
    private IWorkflowFeignService workflowFeignService;


    /**
     * 查询会员详情 - 会员基本信息
     * @param headers HttpHeaders信息
     * @param validateVO 接口参数
     * @return 操作结果
     */
    @Override
    public Wrapper<PlatformMemberDetailBasicVO> getMemberDetailBasic(HttpHeaders headers, MemberValidateVO validateVO) {
        MemberDO memberDO = memberRepository.findById(validateVO.getMemberId()).orElse(null);
        if(memberDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_DOES_NOT_EXIST);
        }

        MemberRelationDO relationDO = relationRepository.findById(validateVO.getValidateId()).orElse(null);
        if(relationDO == null || !relationDO.getSubMemberId().equals(validateVO.getMemberId())) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_RELATION_DOES_NOT_EXIST);
        }

        MemberLevelRightDO levelDO = relationDO.getLevelRight();
        if(levelDO == null) {
            return Wrapper.fail(ResponseCode.MC_MS_MEMBER_LEVEL_DOES_NOT_EXIST);
        }

        PlatformMemberDetailBasicVO basicVO = new PlatformMemberDetailBasicVO();
        //外部流程暂时固定
        List<WorkFlowStepVO> stepVOList = new ArrayList<>();
        WorkFlowStepVO stepVO = new WorkFlowStepVO();
        stepVO.setStep(1);
        stepVO.setStepName("申请会员");
        stepVO.setRoleName(relationDO.getSubRoleName());
        stepVOList.add(stepVO);

        stepVO = new WorkFlowStepVO();
        stepVO.setStep(2);
        stepVO.setStepName("审核会员");
        stepVO.setRoleName(relationDO.getRole().getRoleName());
        stepVOList.add(stepVO);

        //外部审核流程和当前步骤
        basicVO.setOuterVerifySteps(stepVOList);
        if(relationDO.getOuterStatus().equals(MemberOuterStatusEnum.TO_PLATFORM_VERIFY.getCode()) || relationDO.getOuterStatus().equals(MemberOuterStatusEnum.PLATFORM_VERIFYING.getCode())) {
            basicVO.setCurrentOuterStep(1);
        } else {
            basicVO.setCurrentOuterStep(2);
        }

        //内部流程和审核步骤
        Wrapper<WorkflowTaskListBO> taskStepQueryResult = workflowFeignService.listMemberProcessSteps(relationDO);
        if(taskStepQueryResult.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(taskStepQueryResult.getCode(), taskStepQueryResult.getMessage());
        }

        basicVO.setInnerVerifySteps(taskStepQueryResult.getData().getStepList());
        basicVO.setCurrentInnerStep(taskStepQueryResult.getData().getCurrentStep());

        //基本信息
        basicVO.setMemberId(memberDO.getId());
        basicVO.setValidateId(relationDO.getId());
        basicVO.setName(relationDO.getSubMember().getName());
        basicVO.setOuterStatus(relationDO.getOuterStatus());
        basicVO.setOuterStatusName(MemberOuterStatusEnum.getCodeMsg(relationDO.getOuterStatus()));
        basicVO.setInnerStatus(relationDO.getInnerStatus());
        basicVO.setInnerStatusName(PlatformInnerStatusEnum.getCodeMsg(relationDO.getInnerStatus()));
        basicVO.setStatus(relationDO.getStatus());
        basicVO.setStatusName(MemberStatusEnum.getCodeMessage(relationDO.getStatus()));
        basicVO.setMemberTypeName(MemberTypeEnum.getCodeMessage(relationDO.getSubRole().getMemberType().getTypeEnum()));
        basicVO.setRoleName(relationDO.getSubRole().getRoleName());
        basicVO.setLevelTag(levelDO.getLevelTag());
        basicVO.setAccount(memberDO.getAccount());
        basicVO.setPhone(memberDO.getPhone());
        basicVO.setEmail(StringUtils.hasLength(memberDO.getEmail()) ? memberDO.getEmail() : "");
        basicVO.setCreateTime(relationDO.getCreateTime().format(ServiceConfig.DEFAULT_DATETIME_FORMATTER));

        //对资料配置进行分组
        basicVO.setGroups(baseMemberRegisterDetailService.switchMemberRegisterDetailText(memberDO));

        basicVO.setOuterHistory(baseMemberHistoryService.listMemberOuterHistory(relationDO));

        basicVO.setInnerHistory(baseMemberHistoryService.listMemberInnerHistory(relationDO));

        return Wrapper.success(basicVO);
    }
}
